package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.StoreDao;
import cn.ym.mall.entity.Store;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * 赵金明
 * 2022/8/12
 */
public class StoreDaoImpl implements StoreDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增商店
     *
     * @param store 添加的商店
     * @return 返回成功的条数
     */
    @Override
    public Integer addStore(Store store)  throws SQLException {
        int num=0;
        String sql="insert into ym_store(saccount,spwd,sinfoid,sstate) value(?,?,?,?);";
        Object arr[] ={store.getSid(),store.getSaccount(),store.getSpwd(),store.getSinfoid(),store.getSstate()};
        num=queryRunner.update(DruidUtils.getConnection(),sql,arr);
        return num;
    }

    /**
     * 根据商店id删除商店
     *
     * @param sid 订单
     * @return 返回删除条数
     */
    @Override
    public Integer deleteBySid(Integer sid) throws SQLException{
        int num = 0;
        String sql = "delete from ym_store where sid = ?;";
        num = queryRunner.update(DruidUtils.getConnection(), sql, sid);
        return num;
    }

    /**
     * 通过商店id查询商店信息
     *
     * @param sid
     * @return
     */
    @Override
    public Store getStoreBySid(Integer sid) throws SQLException{
        String sql = "select sid,saccount,sinfoid,sstate from ym_order where sid = ?;";
        return queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<Store>(Store.class), sid);
    }

    /**
     * 查询商店总数
     *
     * @return
     */
    @Override
    public long getStoreCount() throws SQLException{
        long count = 0;
        String sql = "select count(0) as count from ym_store;";
        count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>());
        return count;
    }

    /**
     * 通过商店编号统计订单总数
     *
     * @param sid 商店编号
     * @return 返回条数
     */
    @Override
    public long getCountBySid(Integer sid) throws SQLException{
        long count = 0;
        String sql = "select count(0) as count from ym_store where sid = ?;";
        count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>(), sid);
        return count;
    }

    /**
     * 修改商店密码
     * @param store 商店
     * @return 返回更新成功的条数
     */
    @Override
    public Integer updateStore(Store store) throws SQLException{
        int num=0;
        String sql="update ym_store set spwd=? where sid=?;";
        Object[]  arr={store.getSid(),store.getSpwd()};
        num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        return num;
    }
}
